package com.promis.widget.dialog;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.promis.util.CollectionUtils;
import com.promis.widget.R;

import java.util.List;

/**
 * Created by jidanbo on 2017/9/7.
 */

public class DefaultMsgListAdapter extends RecyclerView.Adapter<DefaultMsgListAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<String> msgList;

    private OnItemClickListener onItemClickListener;

    public DefaultMsgListAdapter(Context context) {
        this.context = context;
    }

    public DefaultMsgListAdapter(Context context, List<String> msgList) {
        this.context = context;
        this.msgList = msgList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView textView = new TextView(context);
        AbsListView.LayoutParams txtvParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(txtvParams);
        textView.setPadding(30, 15, 30, 15);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setBackgroundResource(R.drawable.shape_stroke_gray_circular3);

        textView.setOnClickListener(this);

        return new ViewHolder(textView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setTag(position);

        if (CollectionUtils.isNullOrEmpty(msgList)) return;
        String msg = msgList.get(position);
        ((TextView) holder.itemView).setText(msg);
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isNullOrEmpty(msgList)) return 8;
        else return msgList.size();
    }

    public void notifyDataSetChanged(List<String> msgList) {
        this.msgList = msgList;
        this.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (!v.getClass().equals(TextView.class)) return;
        if (onItemClickListener == null) return;
        onItemClickListener.onItemClick(v, ((TextView) v).getText().toString());
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    final static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View view) {
            super(view);
        }
    }

    public static interface OnItemClickListener {

        void onItemClick(View view, String msg);
    }
}